/**
 * @file setting_volume.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_SETTING_VOLUME != 0

/*********************
 *      DEFINES
 *********************/
setting_ringvolume_t settingringvolume;

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
HAL_SPEAKER_GAIN setting_ring_volume_to_hal_speaker_level(uint8_t volume)
{
    HAL_SPEAKER_GAIN halvolume;

    switch(volume) {
        case 0:
            halvolume = HAL_AUDIO_SPK_LEVEL_0;
            break;
        case 1:
            halvolume = HAL_AUDIO_SPK_LEVEL_1;
            break;
        case 2:
            halvolume = HAL_AUDIO_SPK_LEVEL_2;
            break;
        case 3:
            halvolume = HAL_AUDIO_SPK_LEVEL_3;
            break;
        case 4:
            halvolume = HAL_AUDIO_SPK_LEVEL_4;
            break;
        case 5:
            halvolume = HAL_AUDIO_SPK_LEVEL_5;
            break;
        case 6:
            halvolume = HAL_AUDIO_SPK_LEVEL_6;
            break;
        case 7:
            halvolume = HAL_AUDIO_SPK_LEVEL_7;
            break;
        case 8:
            halvolume = HAL_AUDIO_SPK_LEVEL_8;
            break;
        case 9:
            halvolume = HAL_AUDIO_SPK_LEVEL_9;
            break;
        case 10:
            halvolume = HAL_AUDIO_SPK_LEVEL_10;
            break;
        default:
            halvolume = HAL_AUDIO_SPK_LEVEL_0;
            break;
    }

    return(halvolume);
}

uint8_t setting_get_ring_volume(void)
{
    return settingringvolume.ringvolume;
}

void setting_tone_play_onetime_cb(void *para)
{
    printf("%s has been called\n", __FUNCTION__);
    Hal_Audio_Manage_End_Ind(AUDIO_CTRL_PRIORITY_7);
}
void setting_tone_audio_ctrl_callback(AUDIO_CTRL_PRIORITY priority)
{
    printf("%s,priority is %d\n", __FUNCTION__,priority);
    Hal_Audio_Manage_End_Ind(AUDIO_CTRL_PRIORITY_7);
}

void setting_set_ring_volume(uint8_t volume, bool writenvm, bool play)
{
    uint8_t nvdata;

    // update global variable
    settingringvolume.ringvolume = volume;

    // nv write
    if(writenvm) {
        nvdata = volume;
        printf("%s write volume to nvm, %d\n", __FUNCTION__, nvdata);
        UI_NV_Write_Req(NV_SECTION_UI_SETTINGS, 0, 1, &nvdata);
    }

    // hal api
    if(play) {
        if(AUDIO_CTRL_PRIORITY_7 == Hal_Audio_Manage_Start_Req(AUDIO_CTRL_PRIORITY_7,setting_tone_audio_ctrl_callback))
        {
            LV_AUDIO_DECLARE(audio_msg);
            Hal_NFFS_File_Play_Onetime(AUDIO_SETTING_AUDIO_MSG, (HAL_SPEAKER_GAIN)volume, setting_tone_play_onetime_cb, 0);
        }
        else
        {
            Hal_Audio_SetVolume((uint16_t)volume);
        }
    }
}

void setting_volume_init(nv_watch_settings_t * nvsettings)
{
    // init manage variable
    memset(&settingringvolume, 0, sizeof(setting_ringvolume_t));

    settingringvolume.ringvolume = nvsettings->ringvolume;
}

lv_obj_t * setting_volume_create(lv_obj_t * activity_obj)
{
    lv_obj_t * obj;
    lv_obj_t * label;
    lv_obj_t * bar_content;
    lv_obj_t * button_content;

    if(NULL == activity_obj) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_SETTING_VOLUME;
        activity_ext.create = setting_volume_create;
        activity_ext.prepare_destory = setting_volume_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(NULL == activity_obj) {
            return NULL;
        }
    }

    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(NULL == obj) {
        return NULL;
    }

    label = lv_label_create(obj, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN,  &lv_watch_font20_gray);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_click(label, false);
    lv_label_set_text_id(label, WATCH_TEXT_ID_VOLUME_ADJUST);
    lv_obj_align(label, obj, LV_ALIGN_IN_TOP_MID, 0, 10);
    lv_watch_set_free_num(label, 0);

    bar_content = setting_volume_bar_content_create(obj);
    lv_obj_add_style(bar_content, LV_CONT_PART_MAIN,  &lv_watch_style_transp);
    lv_obj_align(bar_content, obj, LV_ALIGN_IN_BOTTOM_MID, 0, -(LV_VER_RES * 3 / 5));
    lv_watch_set_free_num(bar_content, 1);

    button_content = setting_volume_button_content_create(obj);
    lv_obj_add_style(button_content, LV_CONT_PART_MAIN, &lv_watch_style_transp);
    lv_obj_align(button_content, obj, LV_ALIGN_IN_BOTTOM_MID, 0, -(LV_VER_RES  / 4));
    lv_watch_set_free_num(button_content, 2);

    return obj;
}

lv_obj_t * setting_volume_bar_content_create(lv_obj_t * par)
{
    lv_obj_t * content;
    lv_obj_t * img[10];
    uint8_t i;

    if(!par) {
        par = lv_scr_act();
    }

    content = lv_cont_create(par, NULL);
    lv_cont_set_fit(content, LV_FIT_TIGHT);
    lv_cont_set_layout(content, LV_LAYOUT_ROW_BOTTOM);
    LV_ASSERT_MEM(content);
    if(NULL == content) {
        return NULL;
    }

    lv_obj_set_click(content, false);
    lv_watch_obj_add_element(content);

    for(i = 0; i < 10; i++) {
        img[i] = lv_img_create(content, NULL);
        LV_ASSERT_MEM(img[i]);
        if(NULL == img[i]) {
            return NULL;
        }

        if(i < settingringvolume.ringvolume) {
            lv_img_set_src(img[i], ICON_SUB_BOX_BLUE);
        } else {
            lv_img_set_src(img[i], ICON_SUB_BOX_GRAY);
        }
        lv_watch_set_free_num(img[i], i);
        lv_watch_obj_add_element(img[i]);
    }

    return content;
}

lv_obj_t * setting_volume_button_content_create(lv_obj_t * par)
{
    lv_obj_t * content;
    lv_obj_t * btn1;
    lv_obj_t * btn2;
    lv_obj_t * img;

    if(!par) {
        par = lv_scr_act();
    }

    content = lv_cont_create(par, NULL);
    lv_cont_set_fit(content, LV_FIT_TIGHT);
    lv_cont_set_layout(content, LV_LAYOUT_ROW_MID);
    LV_ASSERT_MEM(content);
    if(NULL == content) {
        return NULL;
    }

    lv_obj_set_click(content, true);
    lv_watch_obj_add_element(content);

    btn1 = lv_imgbtn_create(content, NULL);
    lv_imgbtn_set_src(btn1, LV_BTN_STATE_RELEASED, ICON_DECREASE);
    lv_imgbtn_set_src(btn1, LV_BTN_STATE_PRESSED, ICON_DECREASE);
    lv_obj_set_event_cb(btn1, setting_volume_decrease_btn_action);
    lv_watch_obj_add_element(btn1);

    img = lv_img_create(content, NULL);
    LV_ASSERT_MEM(img);
    if(NULL == img) {
        return NULL;
    }
    lv_img_set_src(img, ICON_SPEAKER);
    lv_watch_obj_add_element(img);


    btn2 = lv_imgbtn_create(content, NULL);
    lv_imgbtn_set_src(btn2, LV_BTN_STATE_RELEASED, ICON_INCREASE);
    lv_imgbtn_set_src(btn2, LV_BTN_STATE_PRESSED, ICON_INCREASE);
    lv_obj_set_event_cb(btn2, setting_volume_increase_btn_action);
    lv_watch_obj_add_element(btn2);

    return content;
}

void setting_volume_create_btn_action(lv_obj_t * btn, lv_event_t event)
{
    (void)btn;

    if(LV_EVENT_CLICKED != event) return;

    lv_watch_png_cache_all_free();

    lv_obj_t * obj = setting_volume_create(NULL);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return;
}

void setting_volume_prepare_destory(lv_obj_t * activity_obj)
{
    setting_set_ring_volume(setting_get_ring_volume(), true, false);
    lv_watch_png_cache_all_free();
}

void setting_volume_decrease_btn_action(lv_obj_t * btn, lv_event_t event)
{
    lv_obj_t * par;
    lv_obj_t * child = NULL;
    lv_obj_t * content;
    lv_obj_t * img = NULL;
    uint8_t i;
    uint8_t ringvolume = settingringvolume.ringvolume;

    if(LV_EVENT_PRESSED != event) return;

    content = lv_obj_get_parent(btn);
    par = lv_obj_get_parent(content);

    for(i = 0; i < 3; i++) {
        child = lv_obj_get_child(par, child);
        if(1 == lv_obj_get_user_data(child).user_num) {
            break;
        }
    }

    for(i = 0; i < 10; i++) {
        img = lv_obj_get_child(child, img);
        if(ringvolume <= lv_obj_get_user_data(img).user_num) {
            continue;
        } else {
            lv_img_set_src(img, ICON_SUB_BOX_GRAY);
            ringvolume--;
            setting_set_ring_volume(ringvolume, false, true);
            break;
        }
    }

    return;
}

void setting_volume_increase_btn_action(lv_obj_t * btn, lv_event_t event)
{
    lv_obj_t * par;
    lv_obj_t * child = NULL;
    lv_obj_t * content;
    lv_obj_t * img = NULL;
    uint8_t i;
    uint8_t ringvolume = settingringvolume.ringvolume;

    if(LV_EVENT_PRESSED != event) return;
    content = lv_obj_get_parent(btn);
    par = lv_obj_get_parent(content);

    for(i = 0; i < 3; i++) {
        child = lv_obj_get_child(par, child);
        if(1 == lv_obj_get_user_data(child).user_num) {
            break;
        }
    }

    for(i = 0; i < 10; i++) {
        img = lv_obj_get_child_back(child, img);
        if(ringvolume > lv_obj_get_user_data(img).user_num) {
            continue;
        } else {
            lv_img_set_src(img, ICON_SUB_BOX_BLUE);
            ringvolume++;
            setting_set_ring_volume(ringvolume, false, true);
            break;
        }
    }
}

HAL_SPEAKER_GAIN query_current_volume(void)
{
    return (setting_ring_volume_to_hal_speaker_level(settingringvolume.ringvolume));
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

#endif /*USE_LV_WATCH_SETTING_VOLUME*/
